<template>
  <div class='container' style="padding:10px" >
    <el-card>
    <div v-if="subject.id" slot="header">
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item>学科管理</el-breadcrumb-item>
        <el-breadcrumb-item>大数据</el-breadcrumb-item>
        <el-breadcrumb-item>目录管理</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <el-row>
      <el-col :span="20">
        <el-form :inline="true" :model="directorys" class="demo-form-inline" size="small">
          <el-form-item label="目录名称">
            <el-input v-model="directorys.name" placeholder="目录名称"></el-input>
          </el-form-item>
          <el-form-item label="状态">
            <el-select v-model="directorys.state" >
              <el-option value="已启用"></el-option>
              <el-option value="已禁用"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button @click="clear">清除</el-button>
            <el-button type="primary" @click="onSubmit">搜索</el-button>
          </el-form-item>
       </el-form>
      </el-col>
     <el-col :span=4>
      <div style="display:flex;justify-content:flex-end">
        <el-button type="text" v-if="subject.id" style="margin-left:25px" icon="el-icon-back" @click="$router.back()">返回学科</el-button>
        <el-button type="success" icon="el-icon-edit" size="small" @click="addDirectory">新增目录</el-button>
      </div>
     </el-col>
    </el-row>
    <el-alert
      type="info"
      :closable="false"
      show-icon>
      <template #title>
        数据一共{{total}}条
      </template>
    </el-alert>
    <el-table
    :data="tableData"
    style="width: 100%; margin-top:20px;"
    >
    <el-table-column
      type="index"
      :index="indexMethod"
      label="序号"
      width="80">
    </el-table-column>
    <el-table-column
      prop="subjectName"
      label="所属学科"
      width="120">
    </el-table-column>
    <el-table-column
      prop="directoryName"
      label="目录名称">
    </el-table-column>
    <el-table-column
      prop="username"
      label="创建者">
    </el-table-column>
    <el-table-column
      prop="addDate"
      label="创建日期">
      <template v-slot="{row}">
        {{row.addDate|parseTime}}
      </template>
    </el-table-column>
    <el-table-column
      prop="totals"
      align="center"
      label="面试题数量">
    </el-table-column>
    <el-table-column
      prop="state"
      align="center"
      label="状态">
      <template v-slot={row}>
        <slot>
         {{ row.state===1?'已启用':'已禁用'}}
        </slot>
      </template>
    </el-table-column>
    <el-table-column
      label="操作"
      align="center"
      width="180"
      >
      <template v-slot="{row}">
        <el-button type="text" size="small" @click="toggle(row)">{{row.state===1?'禁用':'启用'}}</el-button>
        <el-button type="text" size="small" :disabled="row.state===1" @click="edit(row)">修改</el-button>
        <el-button type="text" size="small" :disabled="row.state===1" @click="del(row)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
  <div class="block">
    <el-row type="flex" justify="end">
      <el-pagination
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page.sync="page"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="10"
      layout="prev, pager,sizes, next, jumper"
      :total="total">
    </el-pagination>
    </el-row>
  </div>
  </el-card>
    <AddDirectorys :showDialog="showDialog" @getRender="getList" @closeDialog="closeDialog" :row="row"></AddDirectorys>
  </div>
</template>

<script>
import { list, remove, changeState } from '@/api/hmmm/directorys'
import AddDirectorys from '../components/directorys-add'
export default {
  name: 'Directorys',
  components: {
    AddDirectorys
  },
  data () {
    return {
      directorys: {
        name: '',
        state: ''
      },
      tableData: [],
      page: 1,
      pagesize: 10,
      total: 0,
      status: '',
      showDialog: false,
      row: {}
    }
  },
  created () {
    this.getList()
  },
  computed: {
    subject () {
      return this.$route.query || {}
    }
  },
  methods: {
    indexMethod (index) {
      return (this.page - 1) * this.pagesize + index + 1
    },
    addDirectory () {
      this.showDialog = true
      this.row.id = null
    },
    async onSubmit () {
      // if (this.directorys.state === '已启用') {
      //   this.status = 1
      // } else if (this.directorys.state === '已禁用') {
      //   this.status = 0
      // } else {
      //   this.status = null
      // }
      // const res = await list({
      //   page: this.page,
      //   pagesize: this.pagesize,
      //   directoryName: this.directorys.name,
      //   state: this.status
      // })
      // this.tableData = res.data.items
      // this.total = res.data.counts
      this.getList()
    },
    async toggle (row) {
      await changeState({
        id: row.id,
        state: row.state === 1 ? 0 : 1
      })
      this.$message.success('改变状态成功')
      this.getList()
    },
    async getList () {
      if (this.directorys.state === '已启用') {
        this.status = 1
      } else if (this.directorys.state === '已禁用') {
        this.status = 0
      } else {
        this.status = null
      }
      const res = await list({
        page: this.page,
        pagesize: this.pagesize,
        directoryName: this.directorys.name,
        state: this.status
      })
      this.tableData = res.data.items
      this.total = res.data.counts
    },
    clear () {
      this.directorys.name = ''
      this.directorys.state = ''
    },
    async handleCurrentChange (val) {
      this.page = val

      await this.getList()
    },
    async handleSizeChange (val) {
      this.pagesize = val
      this.page = 1
      await this.getList()
    },
    closeDialog () {
      this.showDialog = false
    },
    async edit (row) {
      this.showDialog = true
      this.row = row
    },
    async del (row) {
      await this.$confirm('确认删除吗', '温馨提示', {
        type: 'warning'
      })
      if (this.tableData.length === 1) {
        this.page--
      }
      await remove({
        id: row.id
      })
      this.$message.success('删除成功')
      this.getList()
    }

  }
}
</script>

<style scoped lang='scss'>

</style>
